package kevin.hbase.util;

import com.alibaba.fastjson.JSONObject;
import kevin.hbase.bean.Gps;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.math.RoundingMode;

@Slf4j
public class GpsUtils {
    public static final String amapRegeoUrl = "https://restapi.amap.com/v3/geocode/regeo?";

    public static final String amapKey = "06a0bad772a2f132d17bc24455de2ed6";

    public static final String baiduRegeoUrl = "https://api.map.baidu.com/geocoder/v2/?ak=%s&location=%s&output=json&pois=0&coordtype=wgs84ll";
    public static final String baiduKey = "uDA20omIlYPHDvoLx7sxiubZnfjKFsnS";
    public static final int SCALE = 6;
    public static final BigDecimal D60 = new BigDecimal(60);
    public static final RoundingMode ROUNDING_MODE = RoundingMode.HALF_UP;

    public static String regeoByAMap(double longitude, double latitude) {
        Gps gps = PositionUtil.gps84_To_Gcj02(latitude, longitude);
        if (gps == null) {
            return null;
        }
        String location = gps.getWgLon() + "," + gps.getWgLat();
        String url = amapRegeoUrl + "key=" + amapKey + "&location=" + location;
        try {
            String result = HttpClientPoolUtil.doGet(url, "application/json", null);
            JSONObject jsonObject = JSONObject.parseObject(result);
            Integer status = jsonObject.getInteger("status");
            if (status != null && status == 1) {
                JSONObject regeocode = jsonObject.getJSONObject("regeocode");
                String formattedAddress = regeocode.getString("formatted_address");
                return formattedAddress;
            } else {
                log.error("regeo location:{} error,result:{}", location, result);
                return null;
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    public static String regeoByBaidu(double longitude, double latitude) {
        String location = latitude + "," + longitude;
        String url = String.format(baiduRegeoUrl, baiduKey, location);
        try {
            String result = HttpClientPoolUtil.doGet(url, "application/json", null);
            JSONObject jsonObject = JSONObject.parseObject(result);
            Integer status = jsonObject.getInteger("status");
            if (status != null && status == 0) {
                JSONObject regeocode = jsonObject.getJSONObject("result");
                String formattedAddress = regeocode.getString("formatted_address");
                return formattedAddress;
            } else {
                log.error("regeo location:{} error,result:{}", location, result);
                return null;
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    public static void main(String[] args) {
        //2024-06-28 17:12:12.994 INFO  k.h.s.StopService Line:100 - 372140941465 stop time exceed 3 hours,stopInfo:StopInfo(callLetter=372140941465, stopStartTimeStr=2024-06-26 12:03:12, stopEndTimeStr=2024-06-26 19:08:01, longitude=120.627229, latitude=31.119234, stopTime=25489, refer=, vin=LNAAHAA20P5848032)
        System.out.println(regeoByBaidu(120.627229, 31.119234));
    }
}
